﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace Blueprints.IO
{
    partial class NodeInfoReader
    {
        private sealed class BlueprintReader_NodeCounter
        {
            private readonly Dictionary<int, Dictionary<string, int>> nodeCountsPerDepth = new Dictionary<int, Dictionary<string, int>>();
            private readonly Stack<string> path = new Stack<string>();

            public BlueprintReader_NodeCounter()
            {
                Depth = 0;
                nodeCountsPerDepth[0] = new Dictionary<string, int>(StringComparer.Ordinal);
            }

            public int Depth
            {
                get;
                private set;
            }

            public string Path
            {
                get
                {
                    StringBuilder builder = new StringBuilder();
                    List<string> nodes = new List<string>(path);
                    nodes.Reverse();
                    for (int i = 0; i < Depth; i++)
                    {
                        string name = nodes[i];
                        int count = nodeCountsPerDepth[i][name];
                        builder.Append('/');
                        builder.Append(name);
                        if (count > 1)
                        {
                            builder.Append('[');
                            builder.Append(count);
                            builder.Append(']');
                        }
                    }

                    return builder.ToString();
                }
            }

            public void Push(string name)
            {
                int count;
                if (!nodeCountsPerDepth[Depth].TryGetValue(name, out count))
                {
                    count = 0;
                }

                nodeCountsPerDepth[Depth][name] = ++count;
                path.Push(name);
                Depth++;
                nodeCountsPerDepth[Depth] = new Dictionary<string, int>(StringComparer.Ordinal);
            }

            public void Pop()
            {
                nodeCountsPerDepth.Remove(Depth);
                Depth--;
                path.Pop();
            }
        }
    }
}